import { ApiCall } from "tsrpc";
import { player } from "../../../model/fight/body/player";
import { EffectType, group } from "../../../shared/interface";
import { template } from "../../../shared/master/MsgAction";
import { ReqResLife, ResResLife } from "../../../shared/master/word/PtlResLife";

export default async function (call: ApiCall<ReqResLife, ResResLife>) {
    let user = call.req._player as player;


    let resCont = 0;
    if (user.is_die()) {
        resCont += 1;
    }
    const pets = user.getPets();
    for (let i = 0; i < pets.length; i++) {
        const element = pets[i];
        if (!element.is_die()) {
            continue;
        }
        resCont += 1;
    }
    if (resCont == 0) {
        user.sendMsg(template.default, '你和你的宠物还没有死亡的噢');
        call.succ({ jude: true })
        return;
    }
    // 检测道具是否有复活币
    let log = user.check_wallet(0, 0, [{ id: 'prop-1', v: resCont }], false, '角色/宠物死亡,复活消耗', true);
    if (!log.state) {
        let gold_log = user.check_wallet(80 * resCont, 0, [], false, '复活币不足,金币复活', true)
        if (!gold_log.state) {
            call.succ({ jude: false })
            return;
        }
    }
    if (user.is_die()) {
        user.res_life({
            use_uuid: "0",
            use_name: "主神空间",
            sk_name: "救赎",
            group: group.客场,
            type: EffectType.none,
            val: -1
        })
    }

    for (let i = 0; i < pets.length; i++) {
        const element = pets[i];
        if (!element.is_die()) {
            continue;
        }
        element.res_life({
            use_uuid: "0",
            use_name: "主神空间",
            sk_name: "救赎",
            group: group.客场,
            type: EffectType.none,
            val: -1
        })
    }
    call.succ({ jude: true })
}